package cn.edu.cqvie.atc.stream;

import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 
 * @author zhengsh
 *
 */
public class TestLins {

	public static void main(String[] args) {

		LiLins l1 = new LiLins();
		long t = System.currentTimeMillis();
		l1.setStart(new Date(t));
		l1.setEnd(new Date(t + 1000));

		LiLins l2 = new LiLins();
		l2.setStart(new Date(t));
		l2.setEnd(new Date(t + 2000));

		List<LiLins> li = Stream.of(l1, l2).collect(Collectors.toList());

		AtomicInteger finalS = new AtomicInteger();
		foreach(li, finalS);
		System.out.println(finalS);
	}

	/**
	 * foreach 求和
	 * 
	 * @param li     列表
	 * @param finalS 结果
	 */
	private static void foreach(List<LiLins> li, AtomicInteger finalS) {
		li.stream().forEach(ll -> finalS.addAndGet(getDelta(ll)));
	}

	/**
	 * 定义求和函数
	 * 
	 * @param ll 列表
	 * @return 时间；单位：秒
	 */
	private static int getDelta(LiLins ll) {
		return (int) (ll.getEnd().getTime() - ll.getStart().getTime());
	}
}
